package pw.archipelag.loaderGame.panel.scores {
	import base.uiComponents.ScrollBar;
	import base.uiComponents.scrollArea.MaskWithCovers;
	import flash.display.Sprite;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinScores;
	import base.math.MathExt;
	
	public class ScrollBarScores extends ScrollBar {
		
		private var maskTrack: MaskWithCovers;
		
		public function ScrollBarScores(scrollArea: ScrollAreaScores = null): void {
			ScrollBar.ADDITIONAL_HEIGHT_OPERATIVE_BAR = 10;
			ScrollBar.MARGIN_Y_BAR = scrollArea.heightCover / 2;
			super(scrollArea, 0);
			var countItemsToScroll: uint = this.countItemsToScroll;
		}
		
		override protected function createTrack(): Sprite {
			this.maskTrack = new MaskWithCovers(ScrollAreaScores(this._scrollArea).heightCover);
			this.maskTrack.cacheAsBitmap = true;
			this.addChild(this.maskTrack);
			var track: Sprite = new Sprite();
			track.cacheAsBitmap = true;
			track.mask = this.maskTrack;
			return track;
		}
		
		override protected function createBar(): Sprite {
			var bar: BarScroll = new BarScroll();
			return bar;
		}
		
		private function get countItemsToScroll(): uint {
			var value: uint = Math.max(ScrollAreaScores(this.scrollArea).arrObjResult.length - ScrollScores.COUNT_ITEMS_VISIBLE + 1, 1);
			this.multiplicatorDeltaMouseWheel = ((1 / value) * (this.height - (this.bar.height + 2 * ScrollBar.MARGIN_Y_BAR))) / 3;
			return value;
		}
		
		override protected function getPercentBar(posYBar: Number): Number {
			return Math.round(super.getPercentBar(posYBar) * this.countItemsToScroll) / this.countItemsToScroll;
		}
		
		private function drawTrack(skinScores: SkinScores): void {
			this.maskTrack.width = skinScores.widthScrollBar;
			this.track.scaleX = this.track.scaleY = 1;
			this.track.graphics.clear();
			this.track.graphics.beginFill(skinScores.colorScrollTrack, MathExt.extractChannel(skinScores.colorScrollTrack));
			this.track.graphics.drawRect(0, 0, skinScores.widthScrollBar, 1);
			this.track.graphics.endFill();
		}
		
		private function drawBar(skinScores: SkinScores): void {
			BarScroll(this.bar).draw(skinScores);
			this.bar.x = this.track.x + (this.track.width - this.bar.width) / 2;
			this.originHeightBar = this.bar.height;
		}
		
		public function draw(skinScores: SkinScores): void {
			this.drawTrack(skinScores);
			this.drawBar(skinScores);
		}
		
		override public function set height(value: Number): void {
			this.maskTrack.height = value;
			super.height = value;
		}
		
	}

}